﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace pos
{
    public partial class posStation : application.forms.baseApplication
    {
        public posStation()
        {
            try
            {
                InitializeComponent();
                this.loginRequired = true;
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
        protected override bool ShowLogin()
        {
            int activeFormCount = this.myActiveForms.ActiveFormCount;
            if (activeFormCount > 0)
            {
                this.myActiveForms.ShowAll();
                if (MessageBox.Show("Đóng tất cả " + activeFormCount.ToString() + " cửa số đang mở ?", CommonTypes.Consts.constApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return false;
                if (this.myActiveForms.CloseAll() == false) return false;
            }
            forms.userLogin myLogin = new forms.userLogin();
            myLogin.ShowDialog();
            int permissionCode = 0 , tmpInt =0;
            if (!myLogin.isLoginOk) return false;

            if (application.SysLibs.isSupperAdminLogin())
            {
                CommonTypes.Settings.sysLoginInfo.Roles = (int)CommonTypes.UserRoles.SaleManager;
                permissionCode = (int)common.PermissionCodes.All;
            }
            else
            {
                tmpInt = (int)CommonTypes.UserRoles.SaleManager + (int)CommonTypes.UserRoles.SaleSuppervisor + (int)CommonTypes.UserRoles.PosOperator;
                if ((CommonTypes.Settings.sysLoginInfo.Roles & tmpInt) == 0)
                {
                    common.SysLibs.ShowMessage("Không có quyền sử dụng.");
                    return false;
                }
                permissionCode = DataAccess.DataLibs.GetWorkerAllPermission(CommonTypes.Consts.constSysPermissionMenu,
                                                                            CommonTypes.Settings.sysLoginInfo.UserId, this.Name);
            }
            pos.Settings.myPermission.Set(permissionCode);

            Settings.myPosConfig = Libs.ReadPosConfig();
            configureBtn.Enabled = ((CommonTypes.Settings.sysLoginInfo.Roles & (int)CommonTypes.UserRoles.SaleManager) > 0);
            saleOrderBtn.Enabled = (common.PermissionCodes)permissionCode != common.PermissionCodes.None;
            tmpInt = (int)CommonTypes.UserRoles.SaleManager + (int)CommonTypes.UserRoles.SaleSuppervisor;
            managementBtn.Enabled = (CommonTypes.Settings.sysLoginInfo.Roles & tmpInt) > 0;
            
            return true;
        }

        private void ShowSaleVoucher()
        {
            ShowSaleVoucher(null);
        }
        private void ShowSaleVoucher(string orderId)
        {
            pos.saleOrderRetail myForm = (pos.saleOrderRetail)myActiveForms.Find("saleVoucher");
            if ((myForm == null) || myForm.IsDisposed)
            {
                myForm = new pos.saleOrderRetail();
                myForm.Name = "saleVoucher";
                //myForm.TopMost = true;
            }
            int managementRole = (int)CommonTypes.UserRoles.SaleManager + (int)CommonTypes.UserRoles.SaleSuppervisor;
            myForm.isAdminMode = (CommonTypes.Settings.sysLoginInfo.Roles & managementRole) > 0;
            this.ShowForm(myForm);
            myForm.BringToFront();
            if (common.StringLibs.IsNullOrEmpty(orderId) == false) myForm.Goto(orderId);
        }

        private void ShowConfigure()
        {
            pos.configure myForm = (pos.configure)myActiveForms.Find("configure");
            if ((myForm == null) || (myForm.IsDisposed))
            {
                myForm = new pos.configure();
                myForm.Name = "configure";
            }
            this.ShowForm(myForm);
        }
        private void ShowManagement()
        {
            pos.saleOrderMgt myForm = (pos.saleOrderMgt)myActiveForms.Find("saleOrderMgt");
            if ((myForm == null) || (myForm.IsDisposed))
            {
                myForm = new pos.saleOrderMgt();
                myForm.Name = "saleOrderMgt";
                myForm.myShowOrderFunc += new saleOrderMgt.ShowOrder(ShowOrder);
            }
            this.ShowForm(myForm);
        }

        private void ShowOrder(string orderId, string orderType)
        {
            switch (orderType)
            {
                case CommonTypes.SaleConsts.constOrderRetail:
                     ShowSaleVoucher(orderId);
                     break;
            }
            return;
        }

        private void saleOrderBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ShowSaleVoucher();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }

        private void configureBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ShowConfigure();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }

        private void managementBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ShowManagement();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }

        private void logoutBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ShowLogin();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
        }
    }
}
